﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SampleApp.MvcBackend.ComponentModel;

namespace SampleApp.MvcBackend.Commands.Handlers
{
	public class CreateNewCustomerCommandHandler: Jason.Handlers.Commands.AbstractCommandHandler<CreateNewCustomerCommand>
	{
		readonly Data.Model.Customer.Factory factory;
		readonly IRepositoryFactory repositoryFactory;

		public CreateNewCustomerCommandHandler( Data.Model.Customer.Factory factory, IRepositoryFactory repositoryFactory )
		{
			this.factory = factory;
			this.repositoryFactory = repositoryFactory;
		}

		protected override object OnExecute( CreateNewCustomerCommand command )
		{
			using ( var repository = this.repositoryFactory.OpenSession() )
			{
				var customer = factory.CreateNew( command.CustomerName );

				repository.Save( customer );
				repository.CommitChanges();

				return customer.Id;
			}
		}
	}
}